package com.chengn.threadlocal;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class InheritableThreadLocalTest {
    public static void main(String[] args) throws InterruptedException {
        ThreadLocal<Integer> threadLocal = new InheritableThreadLocal<>();

        threadLocal.set(1);
        System.out.println(threadLocal.get());
        new Thread(() -> {
            System.out.println("新建线程get：" + threadLocal.get());
        }).start();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<>());
        for (int i = 0; i < 2; ++i) {
            threadLocal.set(i);
            System.out.println("主方法get：" + threadLocal.get());
            executor.execute(() -> {
                System.out.println("线程池get：" + threadLocal.get());
            });

            TimeUnit.SECONDS.sleep(1);

        }

    }
}
